package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivityPriVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;

public class PriVideoActivity extends BaseAc<ActivityPriVideoBinding> {

    private List<MyVideoBean> listShow = new ArrayList<>();
    private RecordAdapter videoAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_pri_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPriVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPriVideoDelete.setOnClickListener(this);
        mDataBinding.tvImportVideo.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 2));
        videoAdapter = new RecordAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPriVideoDelete://删除
                deleteFile();
                break;
            case R.id.tvImportVideo://导入视频
                startActivity(ImportVideoActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PlayActivity.videoUri = videoAdapter.getItem(position).getPath();
        startActivity(PlayActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacy;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyVideoBean(file.getPath(), file.getName(), MediaUtil.getDuration(file.getPath()), FileUtils.getFileLastModified(file.getPath())));
            }
        }
        if (listShow.size() > 0) {
            videoAdapter.setList(listShow);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.llVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.llVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void deleteFile() {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (MyVideoBean bean : listShow) {
                    FileUtils.delete(bean.getPath());
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

}